package com.system.file.controller;


import com.system.core.base.R;
import com.system.file.common.dto.FolderDTO;
import com.system.file.common.dto.FolderSaveDTO;
import com.system.file.common.entity.File;
import com.system.file.service.FileService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;

import static com.system.core.base.R.fail;
import static com.system.core.base.R.success;

@RestController
@Slf4j
public class FileController {
    @Autowired
    private FileService fileService;
    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    public R<File> upload(
            @RequestParam(name = "source", defaultValue = "inner") String source,
            @RequestParam(name = "userId", required = false) Long userId,
            @RequestParam(value = "folderId", required = false) Long folderId,
            @RequestParam(value = "file") MultipartFile simpleFile) {
        //1，先将文件存在本地,并且生成文件名
        log.info("contentType={}, name={} , sfname={}", simpleFile.getContentType(), simpleFile.getName(), simpleFile.getOriginalFilename());
        // 忽略路径字段,只处理文件类型
        if (simpleFile.getContentType() == null) {
            return fail("文件为空");
        }

        File file = fileService.upload(simpleFile, 2l, source, userId);
        return success(file);
    }


    /**
     * 保存文件夹
     */
    @ApiOperation(value = "新增文件夹", notes = "新增文件夹")
    @RequestMapping(value = "/saveFolder", method = RequestMethod.POST)
    public R<FolderDTO> saveFolder(@Valid @RequestBody FolderSaveDTO folderSaveDto) {
        //2，获取身份
        FolderDTO folder = fileService.saveFolder(folderSaveDto);
        return success(folder);
    }

}
